package com.tiyende.credit.loop.ui.presenter

import android.Manifest
import android.content.Context
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.common.CommonPresenter
import com.tiyende.credit.loop.ui.contract.IViewContract
import org.json.JSONArray
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/28 15:41
 * @Annotation：权限页面
 */
class TiyendePermissionListPresenter(private val iView: IViewContract.ITiyendePermissionListView): CommonPresenter(iView) {

    /**
     * 加载权限声明列表
     */
    fun onTiyendeLoadPermissionList(){
        val list = arrayListOf<JSONObject>()
        val tipsItem = JSONObject()
        tipsItem.put("itemType","itemTips")
        list.add(tipsItem)
        list.add(queryTiyendeMSAGItem(iView.getContext()))
        list.add(queryTiyendePhoneStateItem(iView.getContext()))
        list.add(queryTiyendeAPListItem(iView.getContext()))
        list.add(queryTiyendeCameraItem(iView.getContext()))
        list.add(queryTiyendePhotoAlbumItem(iView.getContext()))
        list.add(queryTiyendeAddressBookItem(iView.getContext()))
        list.add(queryTiyendeStorageItem(iView.getContext()))
        iView.showTiyendePermissionList(list)
    }

    /**
     * sms
     */
    private fun queryTiyendeMSAGItem(context: Context?): JSONObject {
        val item = JSONObject()
        item.put("isSystem",true)
        item.put("permission", Manifest.permission.READ_SMS)
        item.put("title", context?.getString(R.string.tiyende_permission_sms_title))
        val msgArray = JSONArray()
        val obj1 = JSONObject()
        obj1.put("title", "")
        obj1.put("des", context?.getString(R.string.tiyende_permission_sms_reason1))
        msgArray.put(obj1)

        val obj2 = JSONObject()
        obj2.put("title", "")
        obj2.put("des", context?.getString(R.string.tiyende_permission_sms_reason2))
        msgArray.put(obj2)

        val obj3 = JSONObject()
        obj3.put("title", "")
        obj3.put("des", context?.getString(R.string.tiyende_permission_sms_reason3))
        msgArray.put(obj3)
        item.put("msgList",msgArray)
        return item
    }

    /**
     * phone state
     */
    private fun queryTiyendePhoneStateItem(context: Context?): JSONObject {
        val item = JSONObject()
        item.put("isSystem",false)
        item.put("permission", context?.getString(R.string.tiyende_permission_phone_state_title))
        item.put("title", context?.getString(R.string.tiyende_permission_phone_state_title))
        val msgArray = JSONArray()
        val obj1 = JSONObject()
        obj1.put("title", "")
        obj1.put("des", context?.getString(R.string.tiyende_permission_phone_state_reason))
        msgArray.put(obj1)
        item.put("msgList",msgArray)
        return item
    }

    /**
     * app list
     */
    private fun queryTiyendeAPListItem(context: Context?): JSONObject {
        val item = JSONObject()
        item.put("isSystem",false)
        item.put("permission",context?.getString(R.string.tiyende_permission_app_list_title))
        item.put("title", context?.getString(R.string.tiyende_permission_app_list_title))
        val msgArray = JSONArray()
        val obj1 = JSONObject()
        obj1.put("title", context?.getString(R.string.tiyende_permission_app_list_why))
        obj1.put("des", context?.getString(R.string.tiyende_permission_app_list_why_reason))
        msgArray.put(obj1)

        val obj2 = JSONObject()
        obj2.put("title", context?.getString(R.string.tiyende_permission_app_list_do))
        obj2.put("des", context?.getString(R.string.tiyende_permission_app_list_do_reason))
        msgArray.put(obj2)

        val obj3 = JSONObject()
        obj3.put("title",  context?.getString(R.string.tiyende_permission_app_list_collect))
        obj3.put("des", context?.getString(R.string.tiyende_permission_app_list_collect_reason))
        msgArray.put(obj3)

        val obj4 = JSONObject()
        obj4.put("title",  context?.getString(R.string.tiyende_permission_app_list_data_safety))
        obj4.put("des", context?.getString(R.string.tiyende_permission_app_list_data_safety_reason))
        msgArray.put(obj4)
        item.put("msgList",msgArray)
        return item
    }

    /**
     * camera
     */
    private fun queryTiyendeCameraItem(context: Context?): JSONObject {
        val item = JSONObject()
        item.put("isSystem",false)
        item.put("permission", context?.getString(R.string.tiyende_permission_camera_title))
        item.put("title", context?.getString(R.string.tiyende_permission_camera_title))
        item.put("icon",R.mipmap.tiyende_permission_phone_state_icon)
        val msgArray = JSONArray()
        val obj1 = JSONObject()
        obj1.put("title", "")
        obj1.put("des", context?.getString(R.string.tiyende_permission_camera_reason1))
        msgArray.put(obj1)

        val obj2 = JSONObject()
        obj2.put("title", "")
        obj2.put("des", context?.getString(R.string.tiyende_permission_camera_reason2))
        msgArray.put(obj2)

        val obj3 = JSONObject()
        obj3.put("title", "")
        obj3.put("des", context?.getString(R.string.tiyende_permission_camera_reason3))
        msgArray.put(obj3)
        item.put("msgList",msgArray)
        return item
    }

    /**
     * album
     */
    private fun queryTiyendePhotoAlbumItem(context: Context?): JSONObject {
        val item = JSONObject()
        item.put("isSystem",false)
        item.put("permission", context?.getString(R.string.tiyende_permission_album_title))
        item.put("title", context?.getString(R.string.tiyende_permission_album_title))
        val msgArray = JSONArray()
        val obj1 = JSONObject()
        obj1.put("title", "")
        obj1.put("des", context?.getString(R.string.tiyende_permission_album_reason1))
        msgArray.put(obj1)

        val obj2 = JSONObject()
        obj2.put("title", "")
        obj2.put("des", context?.getString(R.string.tiyende_permission_album_reason2))
        msgArray.put(obj2)
        item.put("msgList",msgArray)
        return item
    }

    /**
     * address book
     */
    private fun queryTiyendeAddressBookItem(context: Context?): JSONObject {
        val item = JSONObject()
        item.put("isSystem",false)
        item.put("permission", context?.getString(R.string.tiyende_permission_address_book_title))
        item.put("title", context?.getString(R.string.tiyende_permission_address_book_title))
        val msgArray = JSONArray()
        val obj1 = JSONObject()
        obj1.put("title", "")
        obj1.put("des", context?.getString(R.string.tiyende_permission_address_book_reason1))
        msgArray.put(obj1)

        val obj2 = JSONObject()
        obj2.put("title", "")
        obj2.put("des", context?.getString(R.string.tiyende_permission_address_book_reason2))
        msgArray.put(obj2)
        item.put("msgList",msgArray)
        return item
    }

    /**
     * storage
     */
    private fun queryTiyendeStorageItem(context: Context?): JSONObject {
        val item = JSONObject()
        item.put("isSystem",false)
        item.put("permission", context?.getString(R.string.tiyende_permission_storage_title))
        item.put("title", context?.getString(R.string.tiyende_permission_storage_title))
        val msgArray = JSONArray()
        val obj1 = JSONObject()
        obj1.put("title", "")
        obj1.put("des", context?.getString(R.string.tiyende_permission_storage_reason1))
        msgArray.put(obj1)

        val obj2 = JSONObject()
        obj2.put("title", "")
        obj2.put("des", context?.getString(R.string.tiyende_permission_storage_reason2))
        msgArray.put(obj2)

        val obj3 = JSONObject()
        obj3.put("title", "")
        obj3.put("des", context?.getString(R.string.tiyende_permission_storage_reason3))
        msgArray.put(obj3)
        item.put("msgList",msgArray)
        return item
    }
}